# 导包
from api.login import LoginAPI
from api.course import CourseAPI
from common.assertutil import common_assert

# 创建测试类
class TestCourseAPI:
    # 初始化
    token = None
    course_id = None

    # 前置处理
    def setup(self):
        # ①实例化接口对象
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        # ②登录成功
        response = self.login_api.get_verify_code()
        uuid = response.json().get("uuid")

        login_data = {
            "username": "admin",
            "password": "admin123",
            "code": "2",
            "uuid": uuid
        }
        response = self.login_api.login(login_data)
        # ③提取登录成功token值
        TestCourseAPI.token = response.json().get("token")
        print(TestCourseAPI.token)
    # 后置处理
    def teardown(self):
        pass

    # step01:新增课程成功
    def test01_insert_course(self):
        test_data = {
            "name": "2023.04.10",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "2023.04.10"
        }
        response = self.course_api.inset_course(token666=TestCourseAPI.token,insert_data=test_data)
        common_assert(response)

    # step02:查询课程成功
    def test02_select_course(self):
        response = self.course_api.select_course(token666=TestCourseAPI.token)
        common_assert(response)
        # 提取课程id并保存
        TestCourseAPI.course_id = response.json().get("rows")[0].get("id")
        print(TestCourseAPI.course_id)



    # step03:修改课程成功
    def test03_update_course(self):
        test_data = {
            "id": TestCourseAPI.course_id,
            "name": "客达天下",
            "subject": "6",
            "price": 998,
            "applicablePerson": "2",
            "info": "课程介绍001"
        }
        response = self.course_api.update_course(token666=TestCourseAPI.token, update_data=test_data)
        common_assert(response)

    # step04:删除课程成功
    def test04_delete_course(self):
        response = self.course_api.delete_course(token666=TestCourseAPI.token,course_id=TestCourseAPI.course_id)
        common_assert(response)